package com.kede.app.service.impl;

import com.kede.app.domain.LeescoreOrder;
import com.kede.app.domain.LeescoreOrderGoods;
import com.kede.app.repository.LeescoreOrderRepository;
import com.kede.dictenum.PayStatuDictEnum;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.LeescoreOrderGoodsRepository;
import com.kede.app.service.LeescoreOrderGoodsService;
import com.kede.app.service.dto.LeescoreOrderGoodsDto;
import com.kede.app.service.dto.LeescoreOrderGoodsQueryCriteria;
import com.kede.app.service.mapstruct.LeescoreOrderGoodsMapper;
import org.hibernate.annotations.Proxy;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-05-12
**/
@Service
@RequiredArgsConstructor
@Proxy(lazy = false)
public class LeescoreOrderGoodsServiceImpl implements LeescoreOrderGoodsService {

    private final LeescoreOrderGoodsRepository leescoreOrderGoodsRepository;
    private final LeescoreOrderGoodsMapper leescoreOrderGoodsMapper;
    private final LeescoreOrderRepository leescoreOrderRepository;

    @Override
    public Map<String,Object> queryAll(LeescoreOrderGoodsQueryCriteria criteria, Pageable pageable){
        Page<LeescoreOrderGoods> page = leescoreOrderGoodsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(leescoreOrderGoodsMapper::toDto));
    }

    @Override
    public List<LeescoreOrderGoodsDto> queryAll(LeescoreOrderGoodsQueryCriteria criteria){
        return leescoreOrderGoodsMapper.toDto(leescoreOrderGoodsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LeescoreOrderGoodsDto findById(Integer id) {
        LeescoreOrderGoods leescoreOrderGoods = leescoreOrderGoodsRepository.findById(id).orElseGet(LeescoreOrderGoods::new);
        ValidationUtil.isNull(leescoreOrderGoods.getId(),"LeescoreOrderGoods","id",id);
        return leescoreOrderGoodsMapper.toDto(leescoreOrderGoods);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LeescoreOrderGoodsDto create(LeescoreOrderGoods resources) {
        return leescoreOrderGoodsMapper.toDto(leescoreOrderGoodsRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LeescoreOrderGoods resources) {
        LeescoreOrderGoods leescoreOrderGoods = leescoreOrderGoodsRepository.findById(resources.getId()).orElseGet(LeescoreOrderGoods::new);
        ValidationUtil.isNull( leescoreOrderGoods.getId(),"LeescoreOrderGoods","id",resources.getId());
        leescoreOrderGoods.copy(resources);
        leescoreOrderGoodsRepository.save(leescoreOrderGoods);
        //更新订单下子商品的状态也要更新整个订单的状态， 整个订单的状态是各个子商品状态中的最小值
        //查询这个订单下所有的子商品状态
        List<LeescoreOrderGoods> all = leescoreOrderGoodsRepository.findAllByOrderIdOrderByOrderStatus(resources.getOrderId());
        //将最新值赋值给整个订单
        LeescoreOrder byId = leescoreOrderRepository.getById(resources.getOrderId());
//        因为数据是enum格式，set的时候应该给下标， 但是get的时候取的是值，导致对应状态混乱
        PayStatuDictEnum payStatuDictEnum = PayStatuDictEnum.valueOfCode(byId.getPay());
        byId.setPay(payStatuDictEnum.getEnumIndex());
        byId.setStatus(all.get(0).getOrderStatus());
        leescoreOrderRepository.save(byId);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            leescoreOrderGoodsRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LeescoreOrderGoodsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LeescoreOrderGoodsDto leescoreOrderGoods : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单ID", leescoreOrderGoods.getOrderId());
            map.put("商品ID", leescoreOrderGoods.getGoodsId());
            map.put("购买数量", leescoreOrderGoods.getBuyNum());
            map.put("积分价格", leescoreOrderGoods.getScore());
            map.put("货币价格", leescoreOrderGoods.getMoney());
            map.put("商品名称", leescoreOrderGoods.getGoodsName());
            map.put("商品缩略图", leescoreOrderGoods.getGoodsThumb());
            map.put("创建时间", leescoreOrderGoods.getCreatetime());
            map.put("物流单号", leescoreOrderGoods.getLogisNo());
            map.put("物流名称", leescoreOrderGoods.getLogisName());
            map.put("订单状态", leescoreOrderGoods.getOrderStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}